package com.chenyanqing.io;

import com.sun.tools.javac.util.Assert;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;

public class Resources {

    private  static  String CLASSPATH_ALL_URL_PREFIX="classpath*";


    public  static InputStream getResourceAsStream(String path){
        InputStream resourceAsStream = Resources.class.getClassLoader().getResourceAsStream(path);
        return  resourceAsStream;
    }


    public static InputStream[] getResources(String locationPattern) throws IOException {
        //是否以classpath*开头
        if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
            //是否包含?或者*
            if(locationPattern.contains("*")){
                int prefixEnd = locationPattern.indexOf(":") + 1;
                String substring = locationPattern.substring(prefixEnd);

                if(substring.contains("/")){
                    String []paths = substring.split("/");
                    String folderpah = substring.replace(paths[paths.length-1],"");
                    if(paths[paths.length-1].contains("*")){
                        //遍历folderpath文件，并根据文件后缀名匹配文获取数据流 TODO
                        return null;
                    }else{
                        InputStream [] inputStreams = new InputStream[1];
                        String pathName = substring;
                        inputStreams[0] = Resources.class.getClassLoader().getResourceAsStream(pathName);
                        return  inputStreams;
                    }
                }

                ClassLoader classLoader = Resources.class.getClassLoader();

                URL resource = classLoader.getResource("");

                Enumeration<URL> resourceUrls =  classLoader.getResources(CLASSPATH_ALL_URL_PREFIX) ;
                Enumeration<URL> resourceUrls1= ClassLoader.getSystemResources(CLASSPATH_ALL_URL_PREFIX);

                System.out.println(substring);
            }

            return null;

        }
//        else {
//            // Only look for a pattern after a prefix here
//            // (to not get fooled by a pattern symbol in a strange prefix).
//            int prefixEnd = locationPattern.indexOf(":") + 1;
//            //是否包含?或者*
//            if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
//                // a file pattern
//                return findPathMatchingResources(locationPattern);
//            }
//            else {
//                // a single resource with the given name
//                return new Resource[] {getResourceLoader().getResource(locationPattern)};
//            }
//        }
        return null;
    }



//
//    protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
//        //获取根路径，也就是classpath*:
//        String rootDirPath = determineRootDir(locationPattern);
//        //获取子路径
//        String subPattern = locationPattern.substring(rootDirPath.length());
//        //获取根路径的觉得路径,包含jar包里面的路径
//        Resource[] rootDirResources = getResources(rootDirPath);
//        Set<Resource> result = new LinkedHashSet<>(16);
//        for (Resource rootDirResource : rootDirResources) {
//            rootDirResource = resolveRootDirResource(rootDirResource);
//            URL rootDirUrl = rootDirResource.getURL();
//            if (equinoxResolveMethod != null && rootDirUrl.getProtocol().startsWith("bundle")) {
//                URL resolvedUrl = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirUrl);
//                if (resolvedUrl != null) {
//                    rootDirUrl = resolvedUrl;
//                }
//                rootDirResource = new UrlResource(rootDirUrl);
//            }
//            //判断路径前缀是否为vfs，
//            if (rootDirUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
//                result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirUrl, subPattern, getPathMatcher()));
//            }
//            //判断路径前缀是否为jar，
//            else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
//                result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirUrl, subPattern));
//            }
//            else {
//                result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
//            }
//        }
//        if (logger.isDebugEnabled()) {
//            logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
//        }
//        return result.toArray(new Resource[0]);
//    }
//
//
//    protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
//        Set<Resource> result = new LinkedHashSet<>(16);
//        ClassLoader cl = getClassLoader();
//        //获取当前classpath的路径
//        Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
//        while (resourceUrls.hasMoreElements()) {
//            URL url = resourceUrls.nextElement();
//            result.add(convertClassLoaderURL(url));
//        }
//        //获取当前classpath下的jar根路径
//        if ("".equals(path)) {
//            // The above result is likely to be incomplete, i.e. only containing file system references.
//            // We need to have pointers to each of the jar files on the classpath as well...
//            addAllClassLoaderJarRoots(cl, result);
//        }
//        return result;
//    }

}
